package com.appkubes.fintech.admin.application.risk.data.fund.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.TreeMap;

public class SignUtil {
    private static final Logger logger = LoggerFactory.getLogger(SignUtil.class);

    public static String sign(Map<String, String> params, String secret) {
        Map<String, String> m = new TreeMap();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            m.put(entry.getKey(), entry.getValue());
        }
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : m.entrySet()) {
            sb.append(entry.getKey());
            sb.append("=");
            sb.append(entry.getValue());
            sb.append("&");
        }
        sb.append("appSecret=");
        sb.append(secret);
        logger.info("param to sign = {}", sb.toString());
        String s = DigestUtils.md5Hex(sb.toString()).toUpperCase();
        logger.info("sign = {}", s);
        return s;
    }
}
